<template>
  <div>
    <div style="text-align: center; ">
      <h2 >修改密码</h2>
      <p>初始密码是123456</p>
    </div>
    <!--  修改密码表单-->
    <div>
      <el-form :model="passForm" status-icon :rules="passFormRules" ref="passForm" label-width="100px">
        <el-form-item label="旧密码" prop="currentPass">
          <el-input type="password" v-model="passForm.currentPass" autocomplete="off"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="新密码" prop="password">
          <el-input type="password" v-model="passForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <br>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="passForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <!--按钮-->
      <div slot="footer" class="dialog-footer buttons">
        <el-button type="primary" @click="submitPassForm('passForm')">提交</el-button>
        <el-button @click="resetForm('passForm')">重置</el-button>
      </div>
    </div>

  </div>
</template>

<script>
export default {
  name: "ChangePassWord",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.passForm.password) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      //修改密码表单
      passForm: {
        password: '',
        checkPass: '',
        currentPass: ''
      },
      //修改密码表单规则
      passFormRules: {
        password: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern:/(?!.*\s)(?!^[\u4e00-\u9fa5]+$)(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,32}$/,
            message: "长度为6-32个字符字母，数字或者特殊字符（-_）至少包含两种",trigger:"blur"}
        ],
        checkPass: [
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        currentPass: [
          { required: true, message: '请输入当前密码', trigger: 'blur' },
        ]
      }

    }
  },
  methods: {
    //提交密码表单
    submitPassForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const _this = this
          this.$axios.post('/sys/user/updatePass', this.passForm).then(res => {
            _this.$alert(res.data.msg, '提示', {
              confirmButtonText: '确定',
              callback: action => {
                this.$refs[formName].resetFields();
                this.$axios.post("/logout").then(res => {
                  console.log(res.data.data)
                  //退出要删除数据
                  localStorage.clear()
                  sessionStorage.clear()
                  this.$store.commit("resetState")
                  this.$router.push("/login")
                })
              }
            });
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
  .el-form {
    width: 420px;
    margin: 50px auto;
  }

  .el-descriptions-item.template{
    width:80px;
  }

  .buttons{
    margin: 10px;
    text-align: center;
  }

</style>